﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web.UI;
using System.Collections.Specialized;
using Guru.Web.UI;

namespace Guru.Web.Controls.Asp
{
    public class Container : PlaceHolder, IMultiLanguageLoader
    {
        #region Members
        private HiddenField _uxActiveStatus;
        // Save active status before _uxActiveStatus init
        private string _IsTempActive;
        #endregion

        public Container(): base()
        {
        }

        #region Properties
        public string HeaderText
        {
            get;
            set;
        }
        public string ContentClientID
        {
            get
            {
                return string.Format("{0}_Container_Content", this.ClientID);
            }
        }
        public string ToogleButtonClientID
        {
            get
            {
                return string.Format("{0}_Container_ToogleButton", this.ClientID);
            }
        }
        public bool IsExpanded
        {
            set
            {
                if (this._uxActiveStatus != null)
                {
                    this._uxActiveStatus.Value = value ? "1" : "0";
                    _IsTempActive = string.Empty;
                }
                else
                {
                    _IsTempActive = value ? "1" : "0";
                }
            }
            get
            {
                if (this._uxActiveStatus != null && this._uxActiveStatus.Value.Trim() == "1")
                {
                    return true;
                }
                return false;
            }
        }
        public bool AllowExpandCollapse
        {
            set;
            get;
        }
        public bool HideHeader
        {
            get;
            set;
        }
        #endregion

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.LoadTextFromResource(((GuruPage)this.Page).CurrentLanguage);
            this.AddActiveStatusStateControl();
            
        }       
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (string.IsNullOrEmpty(_uxActiveStatus.Value))
            {
                _uxActiveStatus.Value = "1";
            }
        }
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            string key = string.Empty;
            if (this.HideHeader)
                key = "Guru.Web.Controls.Asp.Container.NoHeader";
            else if (this.AllowExpandCollapse)
                key = "Guru.Web.Controls.Asp.Container";
            else
                key = "Guru.Web.Controls.Asp.Container.NoExpandCollapse";

            string template = HTMLTemplate.GetByKey(key);
            StringBuilder baseHTML = new StringBuilder();
            using (StringWriter twr = new StringWriter(baseHTML))
            {
                using (HtmlTextWriter newWriter = new HtmlTextWriter(twr))
                {
                    base.Render(newWriter);
                    newWriter.Flush();
                    string display = this.IsExpanded ? string.Empty : "display:none";
                    string activeCss = this.IsExpanded ? " active" : string.Empty;
                    writer.Write(string.Format(template, 
                        this.HeaderText,
                        activeCss,
                        this.ToogleButtonClientID, 
                        this.ContentClientID,
                        display,
                        baseHTML,
                        this._uxActiveStatus.ClientID, "{","}"));
                }
            }
        }

        protected void AddActiveStatusStateControl()
        {
            if (_uxActiveStatus == null)
            {
                _uxActiveStatus = new HiddenField();
                _uxActiveStatus.ID = string.Format("{0}_Container_ActiveStatus", this.ID);
                if (!string.IsNullOrEmpty(_IsTempActive))
                {
                    _uxActiveStatus.Value = _IsTempActive;
                    _IsTempActive = string.Empty;
                }
            }
            this.Controls.Add(_uxActiveStatus);
        }
        public void SetText(NameValueCollection text)
        {
            this.HeaderText = text[this.ID];
        }
    }
}
